<template>
  <section id="ai-models-section" class="section light">
    <div class="container">
      <div class="section-title">
        <h2>强大的AI模型</h2>
        <p>集成多种AI模型，满足不同应用场景需求</p>
      </div>

      <!-- 轮播容器 -->
      <div class="carousel-container">
        <!-- 左侧滚动按钮 -->
        <button
          class="carousel-btn carousel-btn-prev"
          @click="prevSlide"
          :disabled="currentIndex === 0"
        >
          <!-- 使用内联SVG确保箭头显示 -->
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M15 18L9 12L15 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>

        <!-- 轮播内容 -->
        <div
          class="carousel-wrapper"
          ref="carouselWrapper"
          @mouseenter="pauseAutoScroll"
          @mouseleave="resumeAutoScroll"
          @touchstart="touchStart"
          @touchmove="touchMove"
          @touchend="touchEnd"
        >
          <div
            class="carousel-track"
            :style="trackStyle"
            ref="carouselTrack"
          >
            <!-- 传统机器学习卡片 -->
            <div
              class="card"
              @click="tryModel('traditional_ml')"
              @mouseenter="setActiveCard('traditional_ml')"
              @mouseleave="setActiveCard(null)"
              :class="{ 'active': activeCard === 'traditional_ml' }"
            >
              <div class="card-corner-icon ml-icon">ML</div>
              <div class="card-header">
                <h3>传统机器学习</h3>
              </div>
              <div class="card-body">
                <img src="/img/ycfx.png" alt="传统机器学习" class="model-image">
                <p>基于统计学习的经典算法，适用于结构化数据分析和预测任务，提供可靠的机器学习解决方案。</p>
                <div class="tag-list">
                  <span class="tag"><i class="fas fa-check"></i>回归预测</span>
                  <span class="tag"><i class="fas fa-check"></i>数据分类</span>
                  <span class="tag"><i class="fas fa-check"></i>数据聚类</span>
                  <span class="tag"><i class="fas fa-check"></i>特征工程</span>
                </div>
              </div>
              <button class="btn btn-primary card-action-btn" @click="tryModel('traditional_ml')">
                <i class="fas fa-play"></i> 立即探索
              </button>
            </div>

            <!-- 深度学习模型卡片 -->
            <div
              class="card"
              @click="tryModel('deep_learning')"
              @mouseenter="setActiveCard('deep_learning')"
              @mouseleave="setActiveCard(null)"
              :class="{ 'active': activeCard === 'deep_learning' }"
            >
              <div class="card-corner-icon dl-icon">DL</div>
              <div class="card-header">
                <h3>深度学习模型</h3>
              </div>
              <div class="card-body">
                <img src="/img/sdxx.png" alt="深度学习模型" class="model-image">
                <p>多层神经网络架构，擅长处理复杂模式识别和特征学习，在图像、语音等领域表现卓越。</p>
                <div class="tag-list">
                  <span class="tag"><i class="fas fa-check"></i>神经网络</span>
                  <span class="tag"><i class="fas fa-check"></i>特征学习</span>
                  <span class="tag"><i class="fas fa-check"></i>模式识别</span>
                  <span class="tag"><i class="fas fa-check"></i>自动编码</span>
                </div>
              </div>
              <button class="btn btn-primary card-action-btn" @click="tryModel('deep_learning')">
                <i class="fas fa-play"></i> 立即探索
              </button>
            </div>

            <!-- 经典统计模型卡片 -->
            <div
              class="card"
              @click="tryModel('classical_stats')"
              @mouseenter="setActiveCard('classical_stats')"
              @mouseleave="setActiveCard(null)"
              :class="{ 'active': activeCard === 'classical_stats' }"
            >
              <div class="card-corner-icon stats-icon">STAT</div>
              <div class="card-header">
                <h3>经典统计模型</h3>
              </div>
              <div class="card-body">
                <img src="/img/jdtj.png" alt="经典统计模型" class="model-image">
                <p>基于数理统计的严谨方法，提供可解释性强的分析结果，适合假设检验和统计推断任务。</p>
                <div class="tag-list">
                  <span class="tag"><i class="fas fa-check"></i>假设检验</span>
                  <span class="tag"><i class="fas fa-check"></i>回归分析</span>
                  <span class="tag"><i class="fas fa-check"></i>时间序列</span>
                  <span class="tag"><i class="fas fa-check"></i>贝叶斯推断</span>
                </div>
              </div>
              <button class="btn btn-primary card-action-btn" @click="tryModel('classical_stats')">
                <i class="fas fa-play"></i> 立即探索
              </button>
            </div>

            <!-- 预训练模型卡片 -->
            <div
              class="card"
              @click="tryModel('pretrained')"
              @mouseenter="setActiveCard('pretrained')"
              @mouseleave="setActiveCard(null)"
              :class="{ 'active': activeCard === 'pretrained' }"
            >
              <div class="card-corner-icon pt-icon">PT</div>
              <div class="card-header">
                <h3>预训练模型</h3>
              </div>
              <div class="card-body">
                <img src="/img/yxlmx.png" alt="预训练模型" class="model-image">
                <p>基于海量数据预训练的基础模型，支持迁移学习，可快速适配各种下游任务，减少训练成本。</p>
                <div class="tag-list">
                  <span class="tag"><i class="fas fa-check"></i>迁移学习</span>
                  <span class="tag"><i class="fas fa-check"></i>微调适配</span>
                  <span class="tag"><i class="fas fa-check"></i>多任务学习</span>
                  <span class="tag"><i class="fas fa-check"></i>知识蒸馏</span>
                </div>
              </div>
              <button class="btn btn-primary card-action-btn" @click="tryModel('pretrained')">
                <i class="fas fa-play"></i> 立即探索
              </button>
            </div>

            <!-- 生成式AI大模型卡片 -->
            <div
              class="card"
              @click="tryModel('generative_ai')"
              @mouseenter="setActiveCard('generative_ai')"
              @mouseleave="setActiveCard(null)"
              :class="{ 'active': activeCard === 'generative_ai' }"
            >
              <div class="card-corner-icon gen-icon">GEN</div>
              <div class="card-header">
                <h3>生成式AI大模型</h3>
              </div>
              <div class="card-body">
                <img src="/img/scsai.png" alt="预训练模型" class="model-image">
                <p>先进的生成式人工智能，具备强大的内容创作能力，支持文本、图像、代码等多种生成任务。</p>
                <div class="tag-list">
                  <span class="tag"><i class="fas fa-check"></i>内容生成</span>
                  <span class="tag"><i class="fas fa-check"></i>对话交互</span>
                  <span class="tag"><i class="fas fa-check"></i>多模态生成</span>
                  <span class="tag"><i class="fas fa-check"></i>创意设计</span>
                </div>
              </div>
              <button class="btn btn-primary card-action-btn" @click="tryModel('generative_ai')">
                <i class="fas fa-check"></i> 立即探索
              </button>
            </div>
          </div>
        </div>

        <!-- 右侧滚动按钮 -->
        <button
          class="carousel-btn carousel-btn-next"
          @click="nextSlide"
          :disabled="currentIndex >= totalCards - visibleCards"
        >
          <!-- 使用内联SVG确保箭头显示 -->
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M9 18L15 12L9 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>

        <!-- 轮播指示器 -->
        <div class="carousel-indicators">
          <button
            v-for="i in totalIndicators"
            :key="i"
            class="indicator"
            :class="{ 'active': currentIndicator === i - 1 }"
            @click="goToSlide(i - 1)"
          ></button>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
export default {
  name: 'AIModelsCarousel',
  data() {
    return {
      activeCard: null,
      currentIndex: 0,
      totalCards: 5,
      visibleCards: 3,
      autoScrollInterval: null,
      autoScrollDelay: 3000, // 3秒自动滚动
      resizeTimeout: null,
      // 触摸滑动相关
      touchStartX: 0,
      touchEndX: 0,
      isDragging: false,
      dragDistance: 0,
      isTransition: true
    }
  },
  computed: {
    totalIndicators() {
      return Math.max(1, this.totalCards - this.visibleCards + 1);
    },
    currentIndicator() {
      return this.currentIndex;
    },
    trackStyle() {
      // 确保每张卡片完整显示，不被遮住一半
      const cardWidth = 100 / this.visibleCards;
      const translateX = -this.currentIndex * cardWidth;
      return {
        transform: `translateX(${translateX}%)`,
        transition: this.isTransition ? 'transform 0.5s ease-in-out' : 'none'
      };
    }
  },
  mounted() {
    this.updateVisibleCards();
    this.startAutoScroll();
    window.addEventListener('resize', this.handleResize);
  },
  beforeUnmount() {
    this.stopAutoScroll();
    window.removeEventListener('resize', this.handleResize);
    if (this.resizeTimeout) {
      clearTimeout(this.resizeTimeout);
    }
  },
  methods: {
    tryModel(modelType) {
      console.log('尝试模型:', modelType);
      this.$router.push({
        path: '/model-intro/language',
        query: { preset: modelType }
      });
    },
    setActiveCard(cardType) {
      this.activeCard = cardType;
    },
    // 触摸滑动处理
    touchStart(e) {
      this.touchStartX = e.touches[0].clientX;
      this.isDragging = true;
      this.pauseAutoScroll();
    },
    touchMove(e) {
      if (!this.isDragging) return;
      this.touchEndX = e.touches[0].clientX;
      this.dragDistance = this.touchStartX - this.touchEndX;
    },
    touchEnd() {
      if (!this.isDragging) return;
      this.isDragging = false;

      // 滑动距离超过50px才切换
      if (this.dragDistance > 50) {
        this.nextSlide();
      } else if (this.dragDistance < -50) {
        this.prevSlide();
      }

      this.dragDistance = 0;
      this.resumeAutoScroll();
    },
    // 轮播控制方法
    prevSlide() {
      this.isTransition = true;
      if (this.currentIndex > 0) {
        this.currentIndex--;
      } else {
        // 循环到最后一页
        this.currentIndex = this.totalCards - this.visibleCards;
      }
      this.restartAutoScroll();
    },
    nextSlide() {
      this.isTransition = true;
      if (this.currentIndex < this.totalCards - this.visibleCards) {
        this.currentIndex++;
      } else {
        // 循环到第一页
        this.currentIndex = 0;
      }
      this.restartAutoScroll();
    },
    goToSlide(index) {
      this.isTransition = true;
      this.currentIndex = index;
      this.restartAutoScroll();
    },
    // 自动播放控制
    startAutoScroll() {
      this.stopAutoScroll();
      this.autoScrollInterval = setInterval(() => {
        this.nextSlide();
      }, this.autoScrollDelay);
    },
    stopAutoScroll() {
      if (this.autoScrollInterval) {
        clearInterval(this.autoScrollInterval);
        this.autoScrollInterval = null;
      }
    },
    restartAutoScroll() {
      this.stopAutoScroll();
      this.startAutoScroll();
    },
    pauseAutoScroll() {
      this.stopAutoScroll();
    },
    resumeAutoScroll() {
      this.startAutoScroll();
    },
    handleResize() {
      // 防抖处理
      if (this.resizeTimeout) {
        clearTimeout(this.resizeTimeout);
      }
      this.resizeTimeout = setTimeout(() => {
        this.updateVisibleCards();
      }, 250);
    },
    updateVisibleCards() {
      // 根据屏幕宽度调整可见卡片数量，确保每张卡片完整显示
      const width = window.innerWidth;
      if (width < 768) {
        this.visibleCards = 1;
      } else if (width < 992) {
        this.visibleCards = 2;
      } else {
        this.visibleCards = 3;
      }

      // 确保当前索引在有效范围内
      if (this.currentIndex > this.totalCards - this.visibleCards) {
        this.currentIndex = Math.max(0, this.totalCards - this.visibleCards);
      }
    }
  }
}
</script>

<style scoped>
/* CSS 变量定义 - Chrome 75支持 */
.section {
  --primary: #3b82f6;
  --primary-light: #60a5fa;
  --primary-dark: #2563eb;
  --secondary: #10b981;
  --accent: #8b5cf6;
  --bg-section: #f8fafc;
  --bg-card: #ffffff;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --border-light: #e2e8f0;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 6px 20px rgba(59, 130, 246, 0.12);
  --shadow-lg: 0 10px 30px rgba(59, 130, 246, 0.15);
  --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.section {
  padding: 60px 0;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
  background: var(--bg-section);
}

.section.light {
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
}

.section.light::before {
  content: '';
  position: absolute;
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.08) 0%, rgba(59, 130, 246, 0) 70%);
  top: -150px;
  right: -100px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
  position: relative;
  z-index: 1;
}

.section-title {
  text-align: center;
  margin-bottom: 50px;
}

.section-title h2 {
  font-size: 2.2rem;
  margin-bottom: 15px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.3;
}

.section-title p {
  color: var(--text-secondary);
  max-width: 700px;
  margin: 0 auto;
  font-size: 1.1rem;
  line-height: 1.5;
}

/* 轮播容器样式 - 确保卡片完整显示 */
.carousel-container {
  position: relative;
  width: 100%;
  margin: 0 auto;
  overflow: hidden;
  padding: 0 20px; /* 添加内边距确保边缘卡片不被截断 */
}

.carousel-wrapper {
  position: relative;
  width: 100%;
  overflow: hidden;
  border-radius: var(--radius-lg);
  cursor: grab;
}

.carousel-wrapper:active {
  cursor: grabbing;
}

.carousel-track {
  display: flex;
  transition: transform 0.5s ease-in-out;
  will-change: transform;
  /* 移除gap属性，使用margin替代以兼容Chrome 75 */
  width: 100%;
}

/* 卡片样式 - 确保每张卡片完整显示 */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: var(--transition);
  height: auto;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-light);
  position: relative;
  min-height: 450px;
  cursor: pointer;
  flex: 0 0 calc(33.333% - 13.333px); /* 3张卡片，考虑间距 */
  min-width: 0; /* 防止flex项目溢出 */
  /* 使用margin-right替代gap */
  margin-right: 20px;
}

/* 最后一张卡片不需要右边距 */
.card:last-child {
  margin-right: 0;
}

.card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.03) 0%, rgba(139, 92, 246, 0.03) 100%);
  opacity: 0;
  transition: var(--transition);
  z-index: 0;
}

.card:hover::before,
.card.active::before {
  opacity: 1;
}

.card:hover,
.card.active {
  transform: translateY(-5px);
  box-shadow: var(--shadow-lg);
}

/* 左上角图标 */
.card-corner-icon {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 50px;
  height: 50px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 700;
  color: var(--primary);
  z-index: 2;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: var(--transition);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* 不同模型的图标颜色 */
.ml-icon {
  background: linear-gradient(135deg, #8b5cf6, #a78bfa);
  color: white;
}

.dl-icon {
  background: linear-gradient(135deg, #3b82f6, #60a5fa);
  color: white;
}

.stats-icon {
  background: linear-gradient(135deg, #10b981, #34d399);
  color: white;
}

.pt-icon {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  color: white;
}

.gen-icon {
  background: linear-gradient(135deg, #ef4444, #f87171);
  color: white;
}

.card:hover .card-corner-icon,
.card.active .card-corner-icon {
  transform: scale(1.1) rotate(5deg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.card-header {
  padding: 30px 24px 20px;
  background: linear-gradient(155deg, var(--primary) 0%, var(--primary-light) 100%);
  color: white;
  position: relative;
  z-index: 1;
  text-align: center;
}

.card-header h3 {
  font-size: 1.3rem;
  font-weight: 600;
  margin: 0;
  line-height: 1.3;
}

.card-body {
  padding: 24px;
  flex-grow: 1;
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
}

/* 图片样式 */
.model-image {
  width: 100%;
  height: 160px;
  border-radius: var(--radius-md);
  margin-bottom: 20px;
  object-fit: cover;
  border: 1px solid var(--border-light);
}

.card-body p {
  color: #000000;
  margin-bottom: 20px;
  line-height: 1.6;
  font-size: 0.95rem;
  flex-grow: 1;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

/* 标签列表样式 */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  /* 使用负边距和padding替代gap */
  margin: -4px;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  background-color: #f1f5f9;
  border-radius: 22px;
  font-size: 0.85rem;
  color: #374151;
  transition: var(--transition);
  border: 1px solid var(--border-light);
  /* 使用margin替代gap */
  margin: 4px;
}

.tag:hover {
  background-color: #e2e8f0;
  transform: translateY(-1px);
}

.tag i {
  color: var(--secondary);
  margin-right: 6px;
  font-size: 0.8rem;
}

/* 立即探索按钮样式 */
.card-action-btn {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 16px 24px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transform: translateY(100%);
  opacity: 0;
  transition: transform 0.4s cubic-bezier(0.25, 0.8, 0.25, 1),
  opacity 0.3s ease,
  background 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3;
  box-shadow: 0 -4px 12px rgba(59, 130, 246, 0.2);
}

.card-action-btn i {
  margin-right: 8px;
  font-size: 0.9rem;
}

.card-action-btn:hover {
  background: var(--primary-dark);
  transform: translateY(0) scale(1.02);
}

.card:hover .card-action-btn,
.card.active .card-action-btn {
  transform: translateY(0);
  opacity: 1;
}

/* 轮播按钮样式 - 改进箭头显示 */
.carousel-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: white;
  border: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: var(--transition);
  color: var(--primary);
}

.carousel-btn:hover:not(:disabled) {
  background: var(--primary);
  color: white;
  transform: translateY(-50%) scale(1.1);
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.3);
}

.carousel-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  transform: translateY(-50%) scale(1);
}

.carousel-btn-prev {
  left: 10px;
}

.carousel-btn-next {
  right: 10px;
}

/* 确保SVG箭头正确显示 */
.carousel-btn svg {
  width: 20px;
  height: 20px;
  transition: var(--transition);
}

.carousel-btn:hover:not(:disabled) svg {
  transform: scale(1.1);
}

/* 轮播指示器样式 */
.carousel-indicators {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  /* 使用负边距和padding替代gap */
  margin-left: -5px;
  margin-right: -5px;
}

.indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: none;
  background: #cbd5e1;
  cursor: pointer;
  transition: var(--transition);
  /* 使用margin替代gap */
  margin: 0 5px;
}

.indicator.active {
  background: var(--primary);
  transform: scale(1.2);
}

.indicator:hover {
  background: var(--primary-light);
  transform: scale(1.1);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .card {
    flex: 0 0 calc(33.333% - 13.333px);
    margin-right: 20px;
  }
}

@media (max-width: 992px) {
  .card {
    flex: 0 0 calc(50% - 10px); /* 平板设备显示2列 */
    margin-right: 20px;
  }

  .carousel-btn {
    width: 40px;
    height: 40px;
  }

  .carousel-btn svg {
    width: 18px;
    height: 18px;
  }
}

@media (max-width: 768px) {
  .section {
    padding: 40px 0;
  }

  .section-title {
    margin-bottom: 40px;
  }

  .section-title h2 {
    font-size: 1.8rem;
  }

  .section-title p {
    font-size: 0.95rem;
  }

  .card {
    flex: 0 0 100%; /* 移动设备显示1列 */
    min-height: 420px;
    margin-right: 0; /* 移动端单列布局不需要右边距 */
  }

  .card-header {
    padding: 25px 20px 15px;
  }

  .card-body {
    padding: 20px;
  }

  .model-image {
    height: 140px;
    margin-bottom: 15px;
  }

  .card-corner-icon {
    top: 15px;
    left: 15px;
    width: 45px;
    height: 45px;
    font-size: 0.9rem;
  }

  .tag {
    font-size: 0.8rem;
    padding: 5px 10px;
  }

  .card-action-btn {
    padding: 14px 20px;
    font-size: 0.95rem;
  }

  .carousel-btn {
    display: none; /* 在移动设备上隐藏导航按钮 */
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 12px;
  }

  .section-title h2 {
    font-size: 1.6rem;
  }

  .section-title p {
    font-size: 0.9rem;
  }

  .card-header h3 {
    font-size: 1.2rem;
  }

  .card-body p {
    font-size: 0.9rem;
  }

  .model-image {
    height: 120px;
    margin-bottom: 12px;
  }

  .card-corner-icon {
    width: 40px;
    height: 40px;
    font-size: 0.8rem;
  }

  .card-action-btn {
    padding: 12px 16px;
    font-size: 0.9rem;
  }

  .tag-list {
    margin: -3px;
  }

  .tag {
    font-size: 0.75rem;
    padding: 4px 8px;
    margin: 3px;
  }

  .carousel-indicators {
    margin-top: 20px;
  }
}
</style>
